import Vue from 'vue'
import Vuex from 'vuex'
import local from './_local.js'
import http from './http.js'

Vue.use(Vuex)

let loginObject = {
  login: function(param) {
    return new Promise((resolve, reject) => {
      http
        .put({
          url: '/system/login',
          param: param.data,
        })
        .then((data) => {
          if (param.rem) {
            local.setItem('LOGIN_INFO', param)
          }
          local.session.setItem('CURRENT_USER', data)
          resolve(data)
        })
    })
  },
  //查询所有用户集合放入session
  userList: function() {
    return new Promise((resolve, reject) => {
      http
        .get({
          url: '/user/market/list',
        })
        .then((data) => {
          local.setItem('USER_LIST', data.userList)
        })
    })
  },
}

const store = new Vuex.Store({
  state: {
    isSpinShow: false,
    isAlreadyLogin: false,
    currentUser: undefined,
    iport: 'http://localhost:9001/iou/',
  },
  mutations: {
    checkLogin(state) {
      let currentUser = local.session.getItem('CURRENT_USER')
      if (currentUser) {
        state.isAlreadyLogin = true
        state.currentUser = currentUser
        return
      }
      return
      let loginParam = local.getItem('LOGIN_INFO')
      if (
        loginParam &&
        loginParam.data &&
        loginParam.data.password &&
        loginParam.data.account
      ) {
        loginObject.login(loginParam).then((data) => {
          state.isAlreadyLogin = true
          state.currentUser = data
        })
      }
    },
    login(state, param) {
      loginObject.login(param).then((data) => {
        state.isAlreadyLogin = true
        state.currentUser = data
        if (local.session.getItem('CURRENT_USER') != null) {
          loginObject.userList()
        }
      })
      setTimeout(() => {
        loginObject.userList()
      }, 800)
    },
    loginOut(state) {
      state.isAlreadyLogin = false
      state.currentUser = undefined
      local.session.removeItem('CURRENT_USER')
      
    },
  },
})
store.local = local

export default store
